const db = require("../../../db");

const addAgreeList = (req, res) => {
  let { userId } = req.body;
  let sql = `SELECT * FROM friendlist a,user b where a.friendId = b.id and a.userId = ${userId} and a.status = 0`;
  db.query(sql, (err, result) => {
    if (err) return res.send({ code: 2, message: err.message });
    if (result.length === 0)
      return res.send({ code: 1, message: "暂无添加请求列表" });
    res.send({ code: 0, data: result });
  });
};

const addAgree = (req, res) => {
  let { userId, friendId } = req.body;
  let sql = `update friendList set status = 1 where userId=${userId} and friendId=${friendId}`;
  db.query(sql, (err, result) => {
    if (err) return res.send({ code: 2, message: err.message });
    if (!result.affectedRows) return res.send({ code: 1, message: "添加失败" });
    res.send({ code: 0, message: "添加成功" });
  });
};

module.exports = {
  addAgreeList,
  addAgree,
};
